package com.cakeclient.clientsoap.contract;

import org.ksoap2.serialization.NullSoapObject;
import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;
import java.util.List;

public class Order extends ContractObject {
    public int id;
    public Integer idClient;
    public SoapArray<ProductType> contentProductType;
    public DeliveryType deliveryType;
    public PaymentType paiementType;
    public boolean isTracked;
    public Address deliveryAddress;
    public Address billingAddress;
    public String comments;

    public Order() {
    }

    public Order(com.cakeclient.Model.Order order) throws Exception {
        if (order.getRemoteID() != null)
            this.id = Integer.valueOf(order.getRemoteID().toString());
        this.idClient = Integer.valueOf(order.getClient().getRemoteID().toString());

        List<com.cakeclient.Model.ProductType> productTypeList = new ArrayList<com.cakeclient.Model.ProductType>();
        for (com.cakeclient.Model.UnitarySale unitarySale : order.getUnitarySaleList()) {
            if (unitarySale.isProduct()) {
                productTypeList.add(unitarySale.getProductType());
            }
        }

        this.contentProductType = new SoapArray<ProductType>(productTypeList.size(), "contentProductType");
        for (int i = 0; i < productTypeList.size(); i++) {                            //TODO : Passer en foreach
            this.contentProductType.add(new ProductType(productTypeList.get(i)));
        }

        //TODO:Product
        //TODO:MenuType

//        this.deliveryType = new DeliveryType(order.getDeliveryType());
//        this.paiementType = new PaymentType(order.getPaymentType());
        if (order.getDeliveryPlace() != null)
            this.deliveryAddress = new Address(order.getDeliveryPlace().getAddress());
        if (order.getInvoicingPlace() != null)
            this.billingAddress = new Address(order.getInvoicingPlace().getAddress());
        this.comments = order.getCorporateComments();


    }

    @Override
    public void fill(SoapObject soapObject) {
        if (!(soapObject.getPropertySafely("id") instanceof NullSoapObject))
            this.id = Integer.parseInt(soapObject.getPropertySafelyAsString("id"));

        //TODO:A faire plus tard
//        Client client = new com.cakeclient.clientsoap.contract.Client();
//        if (!(soapObject.getPropertySafely("client") instanceof NullSoapObject)) {
//            client.fill((SoapObject) soapObject.getPropertySafely("client"));
//        }

        SoapObject s = soapObject.getPropertySafely("contentProductType") instanceof NullSoapObject ? null : (SoapObject) soapObject.getPropertySafely("contentProductType");
        if (s != null) {
            contentProductType = new SoapArray<ProductType>(s.getPropertyCount(), "contentProductType");
            for (int i = 0; i < s.getPropertyCount(); i++) {
                contentProductType.add(i, new ProductType());
                contentProductType.get(i).fill((SoapObject) s.getProperty(i));
            }
        }

        this.deliveryType = new DeliveryType();
        if (!(soapObject.getPropertySafely("deliveryType") instanceof NullSoapObject))
            this.deliveryType.fill((SoapObject) soapObject.getPropertySafely("deliveryType"));

        this.paiementType = new PaymentType();
        if (!(soapObject.getPropertySafely("paiementType") instanceof NullSoapObject))
            this.paiementType.fill((SoapObject) soapObject.getPropertySafely("paiementType"));

        if (!(soapObject.getPropertySafely("isTracked") instanceof NullSoapObject))
            this.isTracked = Boolean.parseBoolean(soapObject.getPropertySafelyAsString("isTracked"));

        this.deliveryAddress = new Address();
        if (!(soapObject.getPropertySafely("deliveryAddress") instanceof NullSoapObject))
            this.deliveryAddress.fill((SoapObject) soapObject.getPropertySafely("deliveryAddress"));

        this.billingAddress = new Address();
        if (!(soapObject.getPropertySafely("billingAddress") instanceof NullSoapObject))
            this.billingAddress.fill((SoapObject) soapObject.getPropertySafely("billingAddress"));

    }
}
